/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Aspose;

/**
 *
 * @author Ahalya
 */

import com.aspose.email.FolderInfo;
import com.aspose.email.MapiMessage;
import com.aspose.email.MessageInfo;
import com.aspose.email.MessageInfoCollection;
import com.aspose.email.PersonalStorage;
import java.io.File;

public class PSTExtractMessages
{
    // Base folder to load and save files used in this demo
    private static String strBaseFolder = "C:\\aspose-email-2.0.0-java\\samples\\resources\\";

    public static void main(String[] args)
    {
        // Set license
        setLicense();

        String pstFileName = strBaseFolder + "Outlook.pst";

        try
        {
            // Load the Outlook PST file
            PersonalStorage pst = PersonalStorage.fromFile(pstFileName);

            // Get the folders and messages information
            FolderInfo folderInfo = pst.getRootFolder();

            // Create a folder for this PST
            String strRootFolderName = pst.getDisplayName().replace(".pst", "") + ".Java";
            new File(strBaseFolder + strRootFolderName).mkdir();

            // Call the recursive method to extract msg files from each folder
            ExtractMsgFiles(folderInfo, pst, strBaseFolder + strRootFolderName);
        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }

    private static void ExtractMsgFiles(FolderInfo folderInfo, PersonalStorage pst, String strPSTFile)
    {
        // Display the folder name
        System.out.println("Folder: " + folderInfo.getDisplayName());

        // Create folder to store the messages
        String folderName = strPSTFile + "\\" + folderInfo.getDisplayName();
        new File(folderName).mkdir();

        // Loop through all the messages in this folder
        MessageInfoCollection messageInfoCollection = folderInfo.getContents();
        for (int i = 0; i < messageInfoCollection.size(); i++)
        {
            MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get(i);
            System.out.println("Saving message " + messageInfo.getSubject() + "....");
            // Get the message in MapiMessage instance
            MapiMessage message = pst.extractMessage(messageInfo);
            // Delete special characters which are invalid to use as windows file name
            String messageName = null;
            if (message.getSubject() == null || message.getSubject().isEmpty() == true)
            {
                messageName = GetRidOfIllegalFileNameCharacters(messageInfo.getEntryIdString());
            }
            else
            {
                messageName = GetRidOfIllegalFileNameCharacters(message.getSubject());
            }

            // Save this message to disk in MSG format
            message.save(folderName + "\\" + messageName + ".msg");
        }

        // Call this method recursively for each subfolder
        if (folderInfo.hasSubFolders() == true)
        {
            for (int i = 0; i < folderInfo.getSubFolders().size(); i++)
            {
                FolderInfo subfolderInfo = (FolderInfo) folderInfo.getSubFolders().get(i);
                ExtractMsgFiles(subfolderInfo, pst, strPSTFile);
            }
        }
    }

    private static String GetRidOfIllegalFileNameCharacters(String strName)
    {
        String strLegalName = strName.replace(":", " ").replace("\\", " ").replace("?", " ").replace("/", " ").replace("|", " ").replace("*", " ").replace("<", " ").replace(">", " ").replace("\t", " ").replace("\"", " ");
        if (strLegalName.length() >= 100)
        {
            strLegalName = strLegalName.substring(0, 100);
        }
        return strLegalName;
    }

    private static void setLicense()
    {
        try
        {
            // Set license. Provide full path and license file name
            com.aspose.email.License licEmail = new com.aspose.email.License();
            licEmail.setLicense(strBaseFolder + "Aspose.Total.Product.Family.lic");
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}
 